package com.nny.alarm.view;

import com.nny.alarm.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Path.Direction;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.MeasureSpec;

public class DialLayer extends View {
	
	private Paint mTextPaint;
	private int mnTextHeight;
	
	private DialViewAdapter mAdapter;
	
	private Bitmap[] mBitmaps;
	private boolean mbSmallMarker;
	
	private int mnMaxTextLength;
	private int mnInnerOffset = 4;
	private int mnOuterOffset = 12;
	private int mnRadius = 218;
	private int mnColorMarked;
	private int mnColorNormal;
	private float mfRadiusRate;
	private float mfMarkWidth;

	public DialLayer(Context context, AttributeSet attrs) {
		super(context, attrs);
		initDailView();
	}


	@Override
	protected void onDraw(Canvas canvas) {
		drawDial(canvas);

	}
	
	public void setRadiusRate(float rate)
	{
		mfRadiusRate = rate;
	}
	
	public void setAdpater(DialViewAdapter adapter)
	{
		mAdapter = adapter;
	}
	
	@SuppressLint("NewApi")
	public void setSoftwareMode()
	{
		if(android.os.Build.VERSION.SDK_INT >= 11){
			setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
	}

	void initDailView()
	{
		setFocusable(true);
		setSoftwareMode();
		
		Resources res = getResources();

		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mTextPaint.setColor(res.getColor(R.color.text_color));
		mTextPaint.setFakeBoldText(true);
		mTextPaint.setSubpixelText(true);
		mTextPaint.setTextAlign(Paint.Align.LEFT);
		mnColorMarked = res.getColor(R.color.dial_text_marked);
		mnColorNormal = res.getColor(R.color.dial_text_normal);

		mnTextHeight = (int) mTextPaint.measureText("yY");
		
		mBitmaps = new Bitmap[5];
		mBitmaps[0] = BitmapFactory.decodeResource(getResources(), R.drawable.dial_graduation_big);
		mBitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.dial_graduation_small);
		
		mbSmallMarker = false;
		mnMaxTextLength = 100;

	}
	
	void drawDial(Canvas canvas)
	{
		int height = getMeasuredHeight();
		int width = getMeasuredWidth();

		int px = width / 2;
		int py = height / 2;
		
		int len = Math.min(px, py);
		
		mnRadius = (int)(len * mfRadiusRate);
		
		//draw markers
		if(mAdapter == null)return;

		canvas.save();
		Path path = new Path();
		path.moveTo(px + mnTextHeight / 2, py - (mnRadius + mnOuterOffset));
		path.lineTo(px + mnTextHeight / 2, py - (mnRadius + mnOuterOffset + mnMaxTextLength));
		
		int count = mAdapter.getCount();
		float degree = 360.0f / (float)count;

		mbSmallMarker = count >= 20;
		int markerIndex = mbSmallMarker ? 1 : 0;
		float markerWidth = mBitmaps[markerIndex].getWidth();
		
		for (int i = 0; i < count; i++) {
			
			mTextPaint.setColor(mAdapter.isMarked(i) ? mnColorMarked : mnColorNormal);			
			canvas.drawBitmap(mBitmaps[markerIndex], px - markerWidth / 2, py - mnRadius + mnInnerOffset, null);
			canvas.drawTextOnPath(mAdapter.getMarkerText(i), path, 0, 0, mTextPaint);
			canvas.rotate(degree, px, py);
		}
		canvas.restore();
	}
}
